package fr.cephb.joperon.tool;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.sleepycat.db.DatabaseException;

import fr.cephb.joperon.core.entities.DTerms;
import fr.cephb.joperon.core.entities.SemNet;
import fr.cephb.joperon.core.entities.XTerms;



public class XMLFactory
	extends BaseXMLFactory
	{

	public XMLFactory(OperonFactory factory) throws DatabaseException
		{
		super(factory);
		}
	
	
	
	protected void appendTid(Node root,String localName,Object value) throws DatabaseException {
		Integer id=Integer.class.cast(value);
		if(id==0) return;
		Element e= createElement(localName, getFactory().getIterm(id));
		e.setAttribute("tid", String.valueOf(id));
		root.appendChild(e);
		}
	
	@Override
	public Element createXTerms(XTerms object) throws DatabaseException {
		Element e= createElement("XTerms");
		appendTid(e, "relation", object.getRelation());
		appendTid(e, "rterm", object.getRterm());
		appendTid(e, "entity", object.getEntity());

		SemNet semNet=null;
		if((semNet=getFactory().getSemNet().get(object.getSemantic()))!=null)
			{
			Element e2= createElement("semantic");
			e2.setAttribute("id", String.valueOf(object.getSemantic()));
			e2.appendChild(createSemNet(semNet));
			e.appendChild(e2);
			}
		else
			{
			appendTid(e, "semantic", object.getSemantic());
			}
		
		appendTid(e, "organism", object.getOrganism());
		appendTid(e, "organelle", object.getOrganelle());
		appendTid(e, "source", object.getSource());
		appendTid(e, "rule", object.getRule());
		appendTid(e, "status", object.getStatus());
		appendTid(e, "frame", object.getFrame());
		
		
		DTerms dt= getFactory().getDTerms().get(object.getRterm());
		if(dt!=null)
			{
			Element e2= createElement("xFrame");
			e2.setAttribute("id", String.valueOf(object.getRterm()));
			e2.appendChild(getDocument().createTextNode(dt.getDef()));
			e.appendChild(e2);
			}
		
		return e;
		}
	
	
	
	}
